package com.example.lzy.listviewsample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

         //获取listview组件
        ListView  listView = (ListView) findViewById(R.id.listview);

        //定义数据源 数组
        final String[] books = {"语文","数学","英语","政治","化学"};

        //将数组包装成arrayadapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,books);

        //为listview设置adapter
        listView.setAdapter(adapter);

        //监听row点击
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(ListViewActivity.this,"爸爸我点击了"+books[i],Toast.LENGTH_SHORT).show();
            }
        });
    }
}
